package com.victor.core.util;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Properties;

/**
 * @author Jiabin.Zhao
 * @version 1.0
 * @date 创建时间：2016年11月29日 上午9:52:19
 * @parameter
 * @return
 * @since
 */
public class HighsetInit {

    private static HashMap<String, String> highsetProperty;

    public HighsetInit() {
        highsetProperty = new HashMap<String, String>();
    }

    static {
        try {
            reloadAll();
        } catch (Exception e) {
            throw new RuntimeException(e.toString());
        }
    }

    private static void reloadAll() {
        if (highsetProperty == null) highsetProperty = new HashMap<String, String>();
        strageMsg();
    }

    private static void strageMsg() {
        HashMap<String, String> map = new HashMap<String, String>();
        Properties p = new Properties();
        try {
            p.load((HighsetInit.class).getClassLoader().getResourceAsStream("config.properties"));

            String key;
            String value;
            for (Enumeration<?> enu = p.propertyNames(); enu.hasMoreElements(); map.put(key, value)) {
                key = (String) enu.nextElement();
                value = p.getProperty(key);
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e.toString());
        } catch (IOException io) {
            throw new RuntimeException(io.toString());
        }
        highsetProperty = map;
    }

    public static int getPageSize(String key) {
        int str = 10;
        if (highsetProperty != null) {
            if (highsetProperty.containsKey(key)) {
                str = Integer.valueOf((String) highsetProperty.get(key)).intValue();
            }

        }
        return str;
    }

    public static String getProperty(String key) {
        String str = "";
        if (highsetProperty != null) {
            if (highsetProperty.containsKey(key)) {
                str = (String) highsetProperty.get(key);
            }

        }
        return str;
    }

    public static Integer getInteger(String key, Integer re) {
        if (highsetProperty != null) {
            if (highsetProperty.containsKey(key)) {
                int str = Integer.valueOf((String) highsetProperty.get(key)).intValue();
                return str;
            }

        }
        return re;
    }

}
